package com.g1.gzbooksingle.controller;

import com.g1.gzbooksingle.common.Result;
import com.g1.gzbooksingle.dto.BookInfoDTO;
import com.g1.gzbooksingle.model.DgBookInfo;
import com.g1.gzbooksingle.model.DgIndexMenu;
import com.g1.gzbooksingle.service.IDgBookInfoService;
import com.g1.gzbooksingle.service.IDgIndexMenuService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 图书信息表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-10-12
 */
@Slf4j
@Api(tags = "图书信息接口")
@RestController
@RequestMapping("/gzbooksingle/dgBookInfo")
public class DgBookInfoController {


    @Autowired
    private IDgBookInfoService iDgBookInfoService;

    @Autowired
    private IDgIndexMenuService dgIndexMenuService;



    /**
     * 查询图书信息
     * @return
     */
    @GetMapping
    @ApiOperation("查询图书信息")
    public List<BookInfoDTO> getList(){
        log.info("-------查询图书信息-------");
        List<BookInfoDTO> list = iDgBookInfoService.getBookInfos();
        return list;
    }

    /**
     * 获取最新图书按时间排序
     * @param pageNum 页数
     * @param pageSize 条数
     * @return
     */
    @GetMapping("/new/{pageNum}/{pageSize}")
    @ApiOperation("获取最新图书按时间排序5条")
    public PageInfo<BookInfoDTO> getAllOrder(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        return iDgBookInfoService.getBookOrder("create_time", pageNum, pageSize);
    }


    /**
     * 通过图书id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过图书id查询")
    public Result getById(@ApiParam(value = "图书id") @PathVariable Integer id){
        DgBookInfo model = iDgBookInfoService.getById(id);
        return Result.ok(model);
    }

    /**
     * 通过图书id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("通过图书id删除")
    public Result deleteBuId(@ApiParam(value = "图书id") @PathVariable Integer id){
        boolean b = iDgBookInfoService.removeById(id);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 插入数据
     * @param model
     * @return
     */
    @PostMapping
    @ApiOperation("插入数据")
    public Result insert(@ApiParam(name = "电影信息对象",value = "传入json格式") @RequestBody DgBookInfo model){
        boolean b = iDgBookInfoService.save(model);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 通过id修改
     * @param model
     * @return
     */
    @PutMapping
    @ApiOperation("通过id修改数据")
    public Result update(@ApiParam(name = "电影信息对象",value = "传入json格式") @RequestBody DgBookInfo model){
        boolean b = iDgBookInfoService.updateById(model);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @GetMapping("/booksByMenu/{indexId}/{pageNum}/{pageSize}")
    @ApiOperation("根据分类id获取图书")
    public PageInfo<BookInfoDTO> getBooksByIndexMenu(@PathVariable Integer indexId,@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        // 获取所有分类集合
        List<DgIndexMenu> indexMenus = dgIndexMenuService.getIndexMenu();
        // 根据分类id获取该分类的实体类
        DgIndexMenu dgIndexMenu = new DgIndexMenu();
        getDgIndexMenu(indexMenus, indexId, dgIndexMenu);
        // 如果没有该分类返回
        if (dgIndexMenu == null) {
            return null;
        }
        // 查询所有子分类的id保存为字符串 (6,7)
        StringBuilder str = new StringBuilder();
        str.append("(").append(dgIndexMenu.getId());
        if (dgIndexMenu.getChildrenMenu() != null) {
            getDgBookInfos(dgIndexMenu.getChildrenMenu(), str);
        }
        str.append(")");
        PageInfo<BookInfoDTO> pageInfo = iDgBookInfoService.getBooksByIndexId(str.toString(),pageNum,pageSize);
        return pageInfo;
    }


    /**
     * 根据分id递归查找分类DgIndexMenu
     * @param dgIndexMenus 分类集合
     * @param indexId 分类id
     * @return 分类实例
     */
    public void getDgIndexMenu(List<DgIndexMenu> dgIndexMenus, Integer indexId, DgIndexMenu dgIndexMenu){
        for (DgIndexMenu index : dgIndexMenus) {
            if (index.getId().equals(indexId)){
                dgIndexMenu.setChildrenMenu(index.getChildrenMenu());
                dgIndexMenu.setAncestorId(index.getAncestorId());
                dgIndexMenu.setCreateTime(index.getCreateTime());
                dgIndexMenu.setId(index.getId());
                dgIndexMenu.setName(index.getName());
                dgIndexMenu.setParentId(index.getParentId());
            }
            if (index.getChildrenMenu() != null) {
                getDgIndexMenu(index.getChildrenMenu(), indexId, dgIndexMenu);
            }

        }
    }


    /**
     *  测试待定
     */
    public void getDgBookInfos(List<DgIndexMenu> indexMenus, StringBuilder str){
        for (DgIndexMenu indexMenu : indexMenus) {
            str.append(",").append(indexMenu.getId());
            if (!indexMenu.getChildrenMenu().isEmpty()) {
                getDgBookInfos(indexMenu.getChildrenMenu(), str);
            }
        }

    }


}
